package com.arlen.centre.service.impl;

import com.arlen.centre.service.EventService;
import com.arlen.centre.service.WxRestoreMesService;
import com.arlen.commom.vo.BaseMessage;
import com.arlen.until.xml.AnalysisUserMes;
import com.thoughtworks.xstream.XStream;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;


@Service
public class WxRestoreMesServiceImpl implements WxRestoreMesService {

    @Autowired
    AnalysisUserMes analysisUserMes;




    public String restoreMes(Map mesMao) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        BaseMessage baseMessage = null;
        String type = (String) mesMao.get("MsgType");
        if (StringUtils.isBlank(type)){
            return null;
        }

        XStream xStream = new XStream();
       /* //如果再次进行代码添加会有太多的改动以及代码会变得又笨又长难以阅读优化，索性加个服务单独处理
        if (type.equals("event")){
            Object o = eventService.analysisEvent(mesMao);
            String name = o.getClass().getName();
            if (Class.forName(name).getSuperclass().newInstance() instanceof BaseMessage){ //instanceof 太过于消耗性能而且这样会使逻辑复杂抽时间单独在controller直接提取
                xStream.processAnnotations(Class.forName(name));
                return xStream.toXML(o);
            }
            return null;
        }
        //-----*/
            BaseMessage message = analysisUserMes.handleMes(mesMao);
            if (null == message){
                return null;
            }
            String name = message.getClass().getName();
            //解析pojo为xml
            xStream.processAnnotations(Class.forName(name));
            return xStream.toXML(message);
    }
}
